﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Procure.Data.Context;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Linq;

namespace Procure.Library
{
    internal static class XmlConverter : Object
    {
        #region Product
        #region ToXPathNavigator(Product)
        internal static XPathNavigator ToXPathNavigator(Product product)
        {
            XDocument y = ToXDocument(product);

            return y.CreateNavigator();
            
        }
        #endregion
        #region ToXDocument(Product)
        public static XDocument ToXDocument(Product product)
        {
            XElement attributes = new XElement("attributes");
            foreach (ProductAttributeValue value in product.ProductAttributeValues)
            {
                attributes.Add(new XElement("attribute",
                                    new XAttribute("id", value.Id),
                                    new XAttribute("attributeId", value.AttributeId),
                                    new XAttribute("attributeName", value.Attribute.Name),
                                    new XAttribute("value", value.AttributeValue)
                                    )
                            );

            }

            XDocument doc = new System.Xml.Linq.XDocument(
                            new XElement("product",
                                new XAttribute("id", product.Id),
                                new XElement("stockNumber", product.StockNumber),
                                new XElement("name", product.Name),
                                new XElement("price", product.Price),
                                new XElement("description", product.Description),
                                new XElement("productType",
                                    new XAttribute("id", product.ProductTypeId),
                                    new XElement("name", product.ProductType.Name)
                                ),
                                new XElement("shippingBase", product.ShippingBase),
                                new XElement("shippingCase", product.ShippingCase),
                                new XElement("shippingUnit", product.ShippingUnit),
                                new XElement("weight", product.Weight),
                                attributes
                            )
            );
            return doc;
        }
        #endregion
        #endregion

        #region Cart
        #region ToXPathNavigator(Cart)
        internal static XPathNavigator ToXPathNavigator(Cart cart)
        {
            XDocument doc = ToXDocument(cart);
            return doc.CreateNavigator();
        }
        #endregion
        #region ToXDocument(Cart)
        public static XDocument ToXDocument(Cart cart)
        {
            XElement products = new XElement("products");
            foreach (CartProduct product in cart.CartProducts)
            {
                products.Add(new XElement("product",
                                    new XAttribute("id", product.Id),
                                    new XAttribute("productId", product.ProductId),
                                    new XAttribute("price", product.Price),
                                    new XAttribute("quantity", product.Quantity),
                                    new XAttribute("total", product.Price * product.Quantity)
                                    )
                            );
            }

            XDocument doc = new XDocument(
                new XElement("cart",
                    new XAttribute("id", cart.Id),
                    new XAttribute("productCount", cart.CartProducts.Count),
                    new XAttribute("itemCount", cart.CartProducts.Sum(cp => cp.Quantity)),
                    new XAttribute("subtotal", cart.CartProducts.Sum(cp => cp.Price * cp.Quantity)),
                    products
                    )
                );

            return doc;
        }
        #endregion
        #endregion

    }
}
